﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebAppMVCTest01.Models;
using WebAppMVCTest01.ViewModels;

#region -- 说明 --
/*************************************************************************************************************
*
* 在 ASP.NET MVC 中有个 Model Binder 的概念:
* * Model Binder 是组合的关系, 根据空间名称执行 (?)
* * 无论请求是否由带参的 Action 方法生成, Model Binder 都会自动执行.
* * Model Binder 会通过方法的元参数迭代, 然后会和接收到的参数名称做对比. 如果匹配, 则响应接收的数据, 并分配给参数.
* * 在 Model Binder 迭代完成后, 将类参数的每个属性名称与接收的数据做对比, 如果匹配, 则响应接收的数据, 并分配给参数.
* * * 如果两个参数是相关联的, 如参数 "Employee emp" 和 "string FirstName",
* * * 那么 FirstName 会被元 FirstName 变量和 emp.FirstName 属性更新.
* 
*************************************************************************************************************/
#endregion


namespace WebAppMVCTest01.Controllers
{
	public class EmployeeController : Controller
	{
		// GET: Employee

		#region 从数据库读取数据到 ViewModel 实例并传递到 View 页面
		public ActionResult Index()
		{
			EmployeeListViewModel empListViewModel = new EmployeeListViewModel();

			List<EFEmployeeViewModel> efEmpViewModels = new List<EFEmployeeViewModel>();

			EmployeeBusinessLayer empBusLayer = new EmployeeBusinessLayer();
			List<EFEmployee> efEmployees = empBusLayer.GetEFEmployees();

			foreach (EFEmployee e in efEmployees)
			{
				EFEmployeeViewModel efEmpViewModel = new EFEmployeeViewModel()
				{
					EmployeeName = $"{e.FirstName} {e.LastName}",
					Salary = e.Salary.ToString("c"),
					SalaryColor = (e.Salary > 15000) ? "yellow" : "green",
				};
				efEmpViewModels.Add(efEmpViewModel);
			}

			empListViewModel.EFEmployees = efEmpViewModels;

			return View("Index", empListViewModel);
		}
		#endregion

		//Test01.8
		public ActionResult CreateEmployee()
		{
			return View("CreateEmployee");
		}

		//Test01.9 获取 View 传到 Controller 的 Post 数据
		//RedirectToAction 生成 RedirectToRouteResult ( ActionResult 的子类), 表示间接响应 (重定向). 浏览器接收到 RedirectToRouteResult 后将以其指定的 Action 方法产生新的请求
		//EmptyResult, 也是 ActionResult 的子类, 当浏览器接收到 EmptyResult 时, 会显示空白屏幕作为响应, 表示无结果
		public ActionResult SaveEmployee([ModelBinder(typeof(MyModelBinder))]EFEmployee efEmp, string BtnSubmit)
		{
			switch (BtnSubmit)
			{
				case "Save Employee":
					if (ModelState.IsValid)
					{
						EmployeeBusinessLayer efEmpBal = new EmployeeBusinessLayer();
						efEmpBal.SaveEmployee(efEmp);
						return RedirectToAction("Index");
					}
					else
						return View("CreateEmployee");
				case "Cancel":
					return RedirectToAction("Index");
				default:
					return new EmptyResult();
			}
		}
	}

	//Test01.9 继承 DefaultModelBinder 并重写 object CreateModel() 方法, 以解决前端页面控件 name 属性与 Model 类属性名称不一致而无法配对的问题
	public class MyModelBinder : DefaultModelBinder
	{
		protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
		{
			string str = controllerContext.RequestContext.HttpContext.Request.Form["Salary"];
			EFEmployee efEmp = new EFEmployee
			{
				FirstName = controllerContext.RequestContext.HttpContext.Request.Form["FirstName"],
				LastName = controllerContext.RequestContext.HttpContext.Request.Form["LastName"],
				Salary = int.TryParse(str, out int intSalary) ? intSalary : 0,
			};
			return efEmp;
		}
	}
}